package br.com.siscov.controller.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.com.siscov.exceptions.SessaoInvalidaException;
import br.com.siscov.model.vo.LoginVo;
import br.com.siscov.util.Constantes;

public class SiscovFilter implements Filter {

	public void init(FilterConfig arg0) throws ServletException {

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		try {
			HttpSession session = ((HttpServletRequest) request).getSession();
			LoginVo loginVo = (LoginVo) session.getAttribute(Constantes.SESSAO_LOGIN);

			if (loginVo == null) {
				throw new SessaoInvalidaException();
			}

			session.setAttribute(Constantes.SESSAO_PERMISSOES, loginVo.getPermissoes());

			chain.doFilter(request, response);
		} catch (SessaoInvalidaException sie) {

		}
	}

	public void destroy() {

	}

}
